# 63. 求字符串中所有整数的最小和
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('line', function(line) {
const numArr = line.split(/[^\d-]+/);
let res = 0;
for(const num of numArr) {
if (num) {
if (num.indexOf('-') === -1) {
res += getNumSum(num);
} else {
const isNeg = num.startsWith('-');
const subNumArr = num.split('-');
for(let i=0; i<subNumArr.length;i++) {
if (subNumArr[i]) {
const ele = parseInt(subNumArr[i]);
if (i === 0) {
res += isNeg ? 0-ele : getNumSum(subNumArr[i]);
} else {
res -= ele;
}
}
}
}
}
}
console.log(res);
});
function getNumSum(num) {
let sum = 0;
for(const c of num) {
sum += parseInt(c);
}
return sum;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38